package controller;
import gui.*;



import java.util.ArrayList;
import model.OOClass;
import model.OOModel;
public class ClassDiagramManager {
	private ArrayList<String>methods;
	private ArrayList<String>attributes;
	private  OOModel ourModel;
	ArrayList<Shape> shape1= new ArrayList<Shape>();//Newly Constructed arrayList
	
	
ClassDiagramManager(OOModel comingModel){
	methods = new ArrayList<String>();
	ourModel = comingModel;
	attributes = new ArrayList<String>();

	}
//to be called to generate the shapes once u create a object 
	public void generateShapes(){
		//this for loop would loop on arraylist of OOclasses x.classes
		for(OOClass A:ourModel.classes){
			methods = new ArrayList<String>();
		    attributes = new ArrayList<String>();
		 
		    extractMethods(A);//send a reference to ooclass
		    extractAttributes(A);
		    
		    ClassShape l1=new ClassShape(A.getName(),attributes,methods);
			
			shape1.add(l1);//new added lines
		}
		// using the Singleton ControllerManager
		ControllerManager.getDefault().getGUI().diagram.drawShapes(shape1);//sending shape1 through this expression
	}
	public void extractMethods(OOClass currentClass){
		String access;
		String currentMethod = null;
		 for(int i=0;i<currentClass.getMethodsCount();i++){
		   currentMethod = new String();
		   access = currentClass.getMethodAt(i).getAccessModifier();//get accessModifier
		   if(access.matches("public"))
		 	  currentMethod = "+";
		   else if(access.matches("private"))
		 	  currentMethod = "-";
		   else if(access.matches("protected"))
		 	  currentMethod = "#";
		   // agalal: returnType is an OOClass... not a string, please update OOClass.java from SVN
		    currentMethod = currentMethod.concat(currentClass.getMethodAt(i).returnType.getName());//get tye
		 	currentMethod = currentMethod.concat(" ");
		 	currentMethod = currentMethod.concat(currentClass.getMethodAt(i).getName());//get name
		 	currentMethod = currentMethod.concat("(");
		 	for(int k=0;k<currentClass.getMethodAt(i).getParametersCount();k++){
		 		// agalal: OOAttribute's type is an OOClass ... not a String any more
				currentMethod = currentMethod.concat(currentClass.getMethodAt(i).getParameterAt(k).getType().getName());
				currentMethod = currentMethod.concat(" ,");	
				 }
		 		currentMethod = currentMethod.concat(")");
		 		
			methods.add(currentMethod);
		}
		      
		}
		public void extractAttributes(OOClass currentClass){
			String access;
			String currentAttribute = null;
			for(int j=0;j<currentClass.getVariablesCount();j++){
			 access =currentClass.getVariableAt(j). getAccessModifier();
			 currentAttribute = new String (); 
			 if(access.matches("public"))
				 currentAttribute = "+";
			 else if(access.matches("private"))
				 currentAttribute = "-";
			 else if(access.matches("protected"))
				 currentAttribute = "#";
		 	// agalal: OOAttribute's type is an OOClass ... not a String any more
			 currentAttribute = currentAttribute.concat(currentClass.getVariableAt(j).getType().getName());
			 currentAttribute = currentAttribute.concat(" ");
			 currentAttribute = currentAttribute.concat(currentClass.getVariableAt(j).getName());
			 attributes.add(currentAttribute);
			}
			
			
		} 
	
}
